/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

/**
 *
 * @author Administrator
 */
public class GameData {

    private static GameData gameData;
    protected static int MAX_MESSAGE = 30;
    protected int gameTypePlay;
    protected int roomId;
    protected int tableId;
    private int maxPlayer;
    private int playerId;
    private int tableHostId;
    private int numberPlayers;
    private int numberPlayerReady;
    protected long myMoney;
    private int theFirtTurn;
    private int myViewId;
    private int gameRule;
    private int firstTurn;
    protected int numberItemBuy;
    protected int topUserType;
    private int myGender;
    private int numberFriends;
    private int numberTopUserInfo;
    private int numberItems;
    private int messageIndex;
    protected int numberMessage;
    protected int avatarId;
    protected boolean isKick;
    protected boolean isHasNewMessage;
    protected boolean isWaitingMarry;
    protected boolean isOutOfMoney;
    //----------------------------------------------------------------------
    protected boolean[] isPlays;
    protected int[] avatarIds;
    protected int[] readyStates;
    protected long[] playerMoneys;
    protected long[] moneyBetInTable;
    protected int[] numberPlayersInTable;
    protected int[] cardInHandId;
    protected int[] playerGender;
    protected int[] gaData;
    protected ItemBuy[] listItemBuy;
    protected String[] chessMapData;
    protected String[] playerName;
    protected String[] bacayData;
    protected String[] tablePass;
    protected Item[] items;
    protected String myName;
    protected PlayerInfo[] listInfos;
    protected UserMessage[] userMessage;
    //----------------------------------------------------------------------
    private String moneyBet;
    protected String dialogString = "";
    private IGameDisplay gameDiplay;
    protected PlayerInfo userInfoShow;
    protected int[] maxPlayerInTables;
    protected int bcCardId;
    protected String setAdString;
    protected String linkUpdate = "http://gamehai.net/setup/fungame.jar";
    private SmsInfo smsInfo;

    public GameData() {

        if (Debug.isTest) {
            initTest();
        }
    }

    public void setSmsInfo(SmsInfo smsInfo) {
        this.smsInfo = smsInfo;
    }

    public SmsInfo getSmsInfo() {
        return this.smsInfo;
    }

    public void addMessage(UserMessage message, int index) {
        userMessage[index] = message;
    }

    public void initListMessage(int numberMessage) {
        userMessage = new UserMessage[MAX_MESSAGE];
        messageIndex = 0;
    }

    public void changeMoney(int id, int moneyChange) {
        if (myViewId < maxPlayer) {
            Debug.d("pre change money :" + this.playerMoneys[id]);
            this.playerMoneys[id] = this.playerMoneys[id] + moneyChange;
            Debug.d("change money :" + this.playerMoneys[id]);
        }
    }

    protected void setGameDisplay(IGameDisplay display) {
        this.gameDiplay = display;
    }

    private void initTest() {
        avatarIds = new int[4];
        playerGender = new int[4];
        for (int i = 0; i < 4; i++) {
            avatarIds[i] = 0;
            playerGender[i] = 1;
        }
        maxPlayer = 4;
        numberPlayers = 4;
        moneyBet = "100";
        numberPlayerReady = 4;
        playerMoneys = new long[]{100, 100, 100, 100};
        moneyBetInTable = new long[ChooseTableScene.NUMBER_TABLES];
        readyStates = new int[maxPlayer];
        isPlays = new boolean[]{true, true, true, true};
        playerName = new String[]{"baby_ngan_999", "saobang200137", "gucci_26_11", "toanwii",};
        cardInHandId = DataTest.testHaPhom23;
        userInfoShow = new PlayerInfo("name test", 100, 0, 10, 10, 10, "thử phát xem nào thử phát xem nào  ");
        userInfoShow.setIsMaried(1);
        userInfoShow.setMateName("vo test adfa dfaasdfadf");
        userInfoShow.initTest();
//        Debug.d("num childs :" + userInfoShow.getNumberChild());
        numberPlayersInTable = new int[ChooseTableScene.NUMBER_TABLES];
        moneyBetInTable = new long[ChooseTableScene.NUMBER_TABLES];
        numberPlayersInTable[0] = 2;
        gaData = new int[]{10, 10, 10, 10, 10, 10, 10, 10, 10, 10};
        userMessage = new UserMessage[DataTest.numMessages];
        for (int i = 0; i < DataTest.numMessages; i++) {
            userMessage[i] = new UserMessage();
            userMessage[i].setUserSend("test " + i);
            userMessage[i].setMessage("noi dung thu " + i);
            userMessage[i].setKind(5);
        }

        items = new Item[16];

        for (int i = 0; i < 16; i++) {
            items[i] = new Item("Thit cho giai den", "1000", "10", "4000");
        }
        //bacay card
        bacayData = new String[]{"RS3", "0", "1", "6", "1", "0", "10"};
        //tien len
        //cardInHandId = new int[]{0, 3, 5, 6, 1, 45, 32, 51, 22, 34, 42, 21, 3};
    }

    protected void setGaData(String[] data) {
        gaData = new int[10];
        for (int i = 1; i < data.length; i++) {
            gaData[i - 1] = Integer.parseInt(data[i]);
        }
    }

    protected void disposeGa() {
        gaData = null;
    }

    public int[] getCardIds() {
        return cardInHandId;
    }

    public void diposeCardIds() {
        cardInHandId = null;
    }

    public void diposeItem() {
        items = null;
    }

    protected void disposeUserInfoShow() {
        userInfoShow = null;
    }

    public static GameData getInstance() {
        if (gameData == null) {
            gameData = new GameData();
        }
        return gameData;
    }

    public void setGameTypePlay(int type) {
        this.gameTypePlay = type;
        switch (type) {
            case Constants.CO_VUA:
                maxPlayer = Constants.NUMBER_CHESS_PLAYER;
                break;
            case Constants.TIEN_LEN:
            case Constants.PHOM:
            case Constants.BA_CAY:
                maxPlayer = Constants.NUMBER_TIEN_LEN_PLAYER;
                break;

        }
    }

    public int getGameTypePlay() {
        return this.gameTypePlay;
    }

    public void setTableMoneyBet(String[] message) {
        if (moneyBetInTable == null) {
            moneyBetInTable = new long[message.length - 1];
        }
        for (int i = 0, l = message.length - 2; i < l; i++) {
            moneyBetInTable[i] = Long.parseLong(message[i + 1]);
        }
    }

    public long [] getMoneyBetOfTable() {
        return moneyBetInTable;
    }

    public void diposeMoneyBetInTable() {
        moneyBetInTable = null;
    }

    public void setNumberPlayer(int number) {
        numberPlayers = number;
    }

    public void setNumberPlayerInTable(String[] message) {
        if (numberPlayersInTable == null) {
            numberPlayersInTable = new int[ChooseTableScene.NUMBER_TABLES];
        }
        for (int i = 0, l = message.length - 1; i < l; i++) {
            numberPlayersInTable[i] = Integer.parseInt(message[i + 1]);
        }
    }

    public int[] getNumberPlayerInTable() {
        return numberPlayersInTable;
    }

    public void diposeNumberPlayerInTable() {
        numberPlayersInTable = null;
    }

    public void setRoomId(int id) {
        this.roomId = id;
    }

    public int getRoomId() {
        return this.roomId;
    }

    public String getMoneyBet() {
        return this.moneyBet;
    }

    public void setMoneyBet(String moneyBet) {
        this.moneyBet = moneyBet;
    }

    public void diposeMoneyBet() {
        moneyBet = null;
    }

    public boolean[] getIsPlay() {
        return this.isPlays;
    }

    public void disposeIsPlay() {
        isPlays = null;
    }

    public long [] getPlayerMoneys() {
        return this.playerMoneys;
    }

    public void disposePlayerMoneys() {
        playerMoneys = null;
    }

    public int[] getPlayerReadyStates() {
        return this.readyStates;
    }

    public void disposeReadyStates() {
        readyStates = null;
    }

    public int getMaxPlayer() {
        return this.maxPlayer;
    }

    public String[] getPlayerName() {
        return this.playerName;
    }

    public void disposePlayerNames() {
        this.playerName = null;
    }

    public int getPlayerId() {
        return this.playerId;
    }

    public int getTableHostId() {
        return this.tableHostId;
    }

    public int getNumberPlayers() {
        return this.numberPlayers;
    }

    public int getNumberPlayerReadys() {
        return this.numberPlayerReady;
    }

    public void setMyMoney(long money) {
        this.myMoney = money;
    }

    public long getMyMoney() {
        return this.myMoney;
    }

    public int getMyGender() {
        return this.myGender;
    }

    protected void initArr() {
        if (isPlays == null) {
            isPlays = new boolean[maxPlayer];
        }
        if (playerName == null) {
            playerName = new String[maxPlayer];
        }
    }

    public void setRoArrData(String[] data) {
        int length = data.length;
        initArr();
        for (int i = 0; i < length - 4; i++) {
            playerName[i] = data[i + 2];
        }
        moneyBet = data[1];
        theFirtTurn = Integer.parseInt(data[length - 1]);
        tableHostId = Integer.parseInt(data[length - 2]);
        Debug.d("first turn :" + theFirtTurn);
        Debug.d("table host :" + tableHostId);
        Debug.d("money bet :" + moneyBet);
        numberPlayers = 0;
        for (int i = 0; i < maxPlayer; i++) {
            if (playerName[i].equals(myName)) {
                playerId = i;
                isPlays[i] = true;
                numberPlayers++;
                Debug.d("user id:" + i);
            } else if (playerName[i].equals("~")) {
                isPlays[i] = false;
                Debug.d("id not play:" + i);
            } else {
                Debug.d("id play:" + i);
                isPlays[i] = true;
                numberPlayers++;
            }
        }


        if (this.gameDiplay != null) {
            this.gameDiplay.setNumberPlayer(numberPlayers);
            this.gameDiplay.setPlayerId(playerId);
            this.gameDiplay.setTableHostId(tableHostId);
        }
    }

    public int getTheFirstTurn() {
        return this.theFirtTurn;
    }

    public void setMyGender(int gender) {
        this.myGender = gender;
    }

    public void setMyName(String name) {
        this.myName = name;
    }

    public String getMyName() {
        Debug.d("my name :" + myName);
        return this.myName;
    }

    public void setMoArrData(String[] data) {
        if (playerMoneys == null) {
            playerMoneys = new long[4];
        }
        for (int i = 0, l = data.length - 1; i < l; i++) {
            playerMoneys[i] = Long.parseLong(data[i + 1]);
        }
    }

    public void diposeMoData() {
        playerMoneys = null;
    }

    public void setReArrData(String[] data) {
        // Debug.d("change re state");
        numberPlayerReady = 0;
        if (readyStates == null) {
            readyStates = new int[data.length - 1];
        }
        for (int i = 0, l = data.length - 1; i < l; i++) {
            readyStates[i] = Integer.parseInt(data[i + 1]);
            if (readyStates[i] == 1) {
                numberPlayerReady++;
            }
            //  Debug.d("re " + i + ":" + reArrData[i]);
        }
        if (gameDiplay != null) {
            gameDiplay.setNumberPlayerReady(numberPlayerReady);
        }
    }

    public void setTableId(int id) {
        this.tableId = id;
    }

    public int getTableId() {
        return this.tableId;
    }

    protected int getReadyState(int id) {
        return readyStates[id];
    }

    protected int getMyReadyState() {
        return readyStates[playerId];
    }

    protected void setMyReadyState(int state) {
        readyStates[playerId] = state;
    }

    public void setInfoData(String[] infoArr) {
        try {
            // info|viewId|name|gender|experience|money|avatarId
            if (playerGender == null) {
                playerGender = new int[maxPlayer];
            }
            if (avatarIds == null) {
                avatarIds = new int[maxPlayer];
            }
            for (int i = 0, l = infoArr.length; i < l - 5; i++) {
                //  Debug.d("name  :" + i + ":" + infoArr[i + 1]);
                if (infoArr[i + 2].equals(myName)) {
                    myViewId = Integer.parseInt(infoArr[i + 1]);
                    Debug.d("view id by user name:" + myViewId);

                }
                int viewId = Integer.parseInt(infoArr[i + 1]);
                if (viewId < maxPlayer) {
                    playerGender[viewId] = Integer.parseInt(infoArr[i + 3]);
                    avatarIds[viewId] = Integer.parseInt(infoArr[i + 6]);
                    Debug.d("avatar id :" + avatarIds[viewId]);
                }
                i += 5;
            }
            if (gameDiplay != null) {
                gameDiplay.setMyViewId(myViewId);
            }
        } catch (Exception ex) {
            Debug.d("info arr length :" + infoArr.length);
        }
    }

    public int getMyViewId() {
        return this.myViewId;
    }

    public void setGameRule(int rule) {
        this.gameRule = rule;
    }

    public int getGameRule() {
        return this.gameRule;
    }

    protected void setKickOut(boolean b) {
        isKick = b;
    }

    protected boolean getKickOut() {
        return isKick;
    }

    protected void setCardInHandData(String[] part) {
        cardInHandId = new int[part.length - 1];
        for (int i = 0, l = cardInHandId.length; i < l; i++) {
            cardInHandId[i] = Integer.parseInt(part[i + 1]);
        }
    }

    protected void disposeCardInHand() {
        cardInHandId = null;
    }

    protected void disposeBacayData() {
        bacayData = null;
    }

    protected void disposeChessMap() {
        chessMapData = null;
    }

    protected void setFirstTurn(int turn) {
        bcCardId = -1;
        this.firstTurn = turn;
    }

    protected int getFirstTurn() {
        return this.firstTurn;
    }

    void diposeGameDisplay() {
        this.gameDiplay = null;
    }

    void disposeListInfos() {
        listInfos = null;
    }

    void setTopUserType(int type) {
        this.topUserType = type;
    }

    int getTopUserType() {
        return this.topUserType;
    }

    public void initTopUserInfo() {
        if (listInfos == null) {
            listInfos = new PlayerInfo[10];
        }
    }

    public void setNumberTopInfo(int number) {
        this.numberTopUserInfo = number;
    }

    protected int getNumberTopUserInfo() {
        return this.numberTopUserInfo;
    }

    void setTopUserInfo(int index, PlayerInfo info) {
        listInfos[index] = info;
    }

    int getNumberFriend() {
        return numberFriends;
    }

    void initListFriend(int numberFriend) {
        this.numberFriends = numberFriend;
        listInfos = new PlayerInfo[numberFriend];
    }

    public void setFriendInfo(int friendIndex, String name, int isOnline, String status, int gender, int isMaried, int avatarId) {
        status = (status.equals("0") ? "" : status);
        Debug.d("name :" + name);
        listInfos[friendIndex] = new PlayerInfo(name, isOnline, status, gender, isMaried, avatarId);
    }

    void setInfoShow(PlayerInfo info) {
        userInfoShow = info;
    }

    void setMarryInfo(int maried) {
//        Debug.d("user info show :" + userInfoShow);
        userInfoShow.setIsMaried(maried);
    }

    void setMateName(String mateName) {
        userInfoShow.setMateName(mateName);
    }

    void setChildNameInfo(String name) {
        userInfoShow.setChildName(name);
    }

    void setChildGenderInfo(int gender) {
        userInfoShow.setChildGender(gender);
    }

    public int getNumberItems() {
        return numberItems;
    }

    protected void setItemArr(String[] data) {
        this.numberItems = (data.length - 1) >> 2;
        Debug.d("number item :" + numberItems);
        this.items = new Item[numberItems];
        for (int i = 0; i < numberItems; i++) {
            items[i] = new Item(data[1 + 4 * i], data[2 + 4 * i], data[3 + 4 * i], data[4 + 4 * i]);
        }
    }

    public void setChessMapData(String[] part) {
        chessMapData = part;
    }

    public void setBaCayData(String[] data) {
        bacayData = data;
    }

    void initShopData(String[] part) {
        // MYI|itemId|isActived|timeLeft
        numberItemBuy = (part.length - 1) / 3;
//        Debug.d("number buy:" + numberItemBuy);
        listItemBuy = new ItemBuy[numberItemBuy];
        int index = 0;
        for (int i = 0; i < numberItemBuy; i++) {
            Debug.d("type :" + Integer.parseInt(part[index + 1]));
            listItemBuy[i] = new ItemBuy(Integer.parseInt(part[index + 1]), Integer.parseInt(part[index + 2]),
                    Integer.parseInt(part[index + 3]));
            index += 3;
        }
    }

    void deleteMessage(int index, int numMessage) {
        for (int i = index; i < numMessage - 1; i++) {
            userMessage[i] = userMessage[i + 1];
        }
        userMessage[numMessage - 1] = null;
    }

    protected void setTablePass(String[] data) {
        if (tablePass == null) {
            tablePass = new String[ChooseTableScene.NUMBER_TABLES];
        }
        System.arraycopy(data, 1, tablePass, 0, data.length - 1);
//        for (int i = 0; i < tablePass.length; i++) {
//            Debug.d("pass :" + tablePass[i]);
//        }
    }

    void setMaxPlayerInTable(String[] data) {
        if (maxPlayerInTables == null) {
            Debug.d("max :" + data.length);
            maxPlayerInTables = new int[ChooseTableScene.NUMBER_TABLES];
        }
        for (int i = 1; i < data.length; i++) {
            maxPlayerInTables[i - 1] = Integer.parseInt(data[i]);
        }
    }

    void setBcCard(int bcCard) {
        this.bcCardId = bcCard;
    }
}
